import { S3 } from "aws-sdk";
import { v4 as uuidv4 } from "uuid";
import S3MinioClient from "./S3MinioClient";

export interface S3FileOptions {
  file: File;
  minioClient: S3MinioClient;
  onSuccess?: () => void;
}

export enum S3FileStatus {
  Wait = "WAIT", // 等待上传
  Uploading = "UPLOADING", // 上传中
  Success = "SUCCESS", // 成功
}

export class S3File {
  id: string; // 唯一标识

  minioClient: S3MinioClient; // minioClient实例

  status: S3FileStatus; // 文件状态

  originalFile: File; // 源文件

  takeTime: { start: number; end: number }; // 耗时

  _onSuccess: (data: S3.ManagedUpload.SendData, file: File) => void; // 成功回调

  constructor(options: S3FileOptions) {
    this.id = uuidv4();
    this.minioClient = options.minioClient;
    this.status = S3FileStatus.Wait;
    this.originalFile = options.file;
    this._onSuccess = options.onSuccess;
  }

  uploadStart() {
    this.takeTime.start = Date.now();
  }

  uploadEnd() {
    this.takeTime.end = Date.now();
  }

  onSuccess(data: S3.ManagedUpload.SendData) {
    this.status = S3FileStatus.Success;

    this._onSuccess?.(data);
  }
}

export default S3File;
